#include "Controller.h"
#include"Viewer.h"

Controller::Controller(void)
{
	m_pMVMap = new multimap<Model*, Viewer*>();
}


Controller::~Controller(void)
{
	delete m_pMVMap;
}

void Controller::Register(Model *pModel, Viewer *pViewer)
{
	m_pMVMap->insert(make_pair(pModel, pViewer));
}

void Controller::UnRegister(Model *pModel, Viewer *pViewer)
{
	multimap<Model*, Viewer*>::iterator ite = m_pMVMap->begin();

	while (ite != m_pMVMap->end())
	{
		if ( pModel==ite->first && pViewer==ite->second)
		{
			ite = m_pMVMap->erase(ite);
		}
		else
		{
			ite++;
		}
	}
}

void Controller::Notify(Model *pModel)
{
	multimap<Model*, Viewer*>::iterator ite ;
	ite = m_pMVMap->lower_bound(pModel);
	while ( ite != m_pMVMap->upper_bound(pModel))
	{
		ite->second->Update(ite->first);
		ite++;
	}
}